package cn.wolfcode.tieba.resolver;

import cn.wolfcode.tieba.domain.User;
import cn.wolfcode.tieba.redis.service.IUserRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 王金龙
 * @date 2021/1/21 19:30
 */
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private IUserRedisService redisService;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType() == User.class;
//        && parameter.hasMethodAnnotation(UserParam.class)
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
        String token = null;
        if (request != null) {
            token = request.getHeader("token");
        }
        return redisService.getUserToken(token);
    }
}
